<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreDataCacheBase extends Base {
	var $name = 'name';
	var $type = 'data';
	var $dateline = 0;
	var $expiration = 0;
	var $data = '';
	var $internal = 3600;
	var $truncate = false;
	
	//constructor
	function CoreDataCacheBase() {
		parent::Base();
		
		$this->internal = 60 * 60 * 24;
	}
	
	//methods
	function getDetail($name, $type = '') {
		$detail = array();
		$data_array = $this->_get_data($name, $type);
		if( is_array($data_array) ) {
			$detail = array(
						'name' => $data_array['caches_name'],
						'type' => $data_array['caches_type'],
						'dateline' => $data_array['caches_name'],
						'expiration' => $data_array['caches_expiration'],
						'data' => $data_array['caches_data'],
							);
		}
		
		return $detail;
	}
	
	function getData($name, $type = '') {
		$data_array = $this->_get_data($name, $type);
		if( is_array($data_array) ) {
			return $data_array['caches_data'];
		}
		
		return NULL;
	}
	
	function addData($data, $name, $type = '') {		
		$has_exists = $this->_check_exists($name, $type);
		if( $has_exists == true ) {
			$this->_update_data($data, $name, $type);
		} else {
			$this->_add_data($data, $name, $type);
		}
	}
	
	function updateData($data, $name, $type = '') {
		$has_exists = $this->_check_exists($name, $type);
		if( $has_exists == true ) {
			$this->_update_data($data, $name, $type);
		} else {
			$this->_add_data($data, $name, $type);
		}
	}
	
	//private
	function _get_data($name, $type = '') {
		$name = func_db_input($name);
		
		$now = time();
		$query = "select * from pp_core_caches where caches_name = '" . $name . "' and caches_dateline <= '" . $now . "' " 
				. " and caches_expiration >= '" . $now . "'";
		if( $type != '' ) {
			$query .= " and caches_type = '" . $type . "'";
		}
		
		return $this->adb->getRow($query);
	}
	
	function _check_exists($name, $type = '') {
		$name = func_db_input($name);
		$query = "select count(*) as count from pp_core_caches where caches_name = '" . $name . "'";
		if( $type != '' ) {
			$query .= " and caches_type = '" . $type . "'";
		}
		
		return $this->adb->checkCount($query);
	}
	
	function _add_data($data, $name, $type = '') {
		$name = func_db_input($name);
		$data = func_db_input($data);
		
		$dateline = time();
		$expiration = $dateline + $this->internal;
		
		$query = "insert into pp_core_caches (caches_name, caches_type, caches_dateline, caches_expiration, caches_data) values " 
				. " ('" . $name . "', '" . $type . "', '" . $dateline . "', '" . $expiration . "', '" . $data . "') ";
		
		return $this->adb->insert($query);
	}
	
	function _update_data($data, $name, $type = '') {
		$name = func_db_input($name);
		$data = func_db_input($data);
		
		$dateline = time();
		$expiration = $dateline + $this->internal;
		
		$query = "update pp_core_caches set caches_dateline = '" . $dateline . "', caches_expiration = '" . $expiration . "', " 
				. " caches_data = '" . $data . "' where caches_name = '" . $name . "'";
		if( $type != '' ) {
			$query .= " and caches_type = '" . $type . "'";
		}
		
		return $this->adb->update($query);
	}
	
	function _remove_data($name, $type = '') {
		$name = func_db_input($name);
		$data = func_db_input($data);
		
		$query = "delete from pp_core_caches where caches_name = '" . $name . "'";
		if( $type != '' ) {
			$query .= " and caches_type = '" . $type . "'";
		}
		
		return $this->adb->delete($query);
	}
	
	function _clean_cache($name, $type = '') {
		$name = func_db_input($name);
		$data = func_db_input($data);
		
		$query = "delete from pp_core_caches where caches_name = '" . $name . "'";
		if( $type != '' ) {
			$query .= " and caches_type = '" . $type . "'";
		}
		
		return $this->adb->delete($query);
	}
	
	function _clean_allcache() {
		if( $this->truncate == true ) {
			return $this->adb->query("truncate table pp_core_caches");
		} else {
			return $this->adb->query("delete from pp_core_caches where 1");
		}
	}
}
//
?>